home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / Noisy.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.9 KB  |  76 lines

  1. //: C20:Noisy.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // A class to track various object activities
  7. #ifndef NOISY_H
  8. #define NOISY_H
  9. #include <iostream>
  10.  
  11. class Noisy {
  12.   static long create, assign, copycons, destroy;
  13.   long id;
  14. public:
  15.   Noisy() : id(create++) { 
  16.     std::cout << "d[" << id << "]"; 
  17.   }
  18.   Noisy(const Noisy& rv) : id(rv.id) {
  19.     std::cout << "c[" << id << "]";
  20.     copycons++;
  21.   }
  22.   Noisy& operator=(const Noisy& rv) {
  23.     std::cout << "(" << id << ")=[" <<
  24.       rv.id << "]";
  25.     id = rv.id;
  26.     assign++;
  27.     return *this;
  28.   }
  29.   friend bool 
  30.   operator<(const Noisy& lv, const Noisy& rv) {
  31.     return lv.id < rv.id;
  32.   }
  33.   friend bool 
  34.   operator==(const Noisy& lv, const Noisy& rv) {
  35.     return lv.id == rv.id;
  36.   }
  37.   ~Noisy() {
  38.     std::cout << "~[" << id << "]";
  39.     destroy++;
  40.   }
  41.   friend std::ostream& 
  42.   operator<<(std::ostream& os, const Noisy& n) {
  43.     return os << n.id;
  44.   }
  45.   friend class NoisyReport;
  46. };
  47.  
  48. struct NoisyGen {
  49.   Noisy operator()() { return Noisy(); }
  50. };
  51.  
  52. // A singleton. Will automatically report the
  53. // statistics as the program terminates:
  54. class NoisyReport {
  55.   static NoisyReport nr;
  56.   NoisyReport() {} // Private constructor
  57. public:
  58.   ~NoisyReport() {
  59.     std::cout << "\n-------------------\n"
  60.       << "Noisy creations: " << Noisy::create
  61.       << "\nCopy-Constructions: " 
  62.       << Noisy::copycons
  63.       << "\nAssignments: " << Noisy::assign
  64.       << "\nDestructions: " << Noisy::destroy
  65.       << std::endl;
  66.   }
  67. };
  68.  
  69. // Because of these this file can only be used
  70. // in simple test situations. Move them to a 
  71. // .cpp file for more complex programs:
  72. long Noisy::create = 0, Noisy::assign = 0,
  73.   Noisy::copycons = 0, Noisy::destroy = 0;
  74. NoisyReport NoisyReport::nr;
  75. #endif // NOISY_H ///:~
  76.